/*
 * @lc app=leetcode.cn id=696 lang=cpp
 *
 * [696] 计数二进制子串
 */

// @lc code=start
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int countBinarySubstrings(string s)
    {
        int ans = 0;
        int l0 = 0, r0 = 1, l1, r1;
        while (r0 < s.size() && s[r0] == s[r0 - 1])
            ++r0;

        while (r0 < s.size())
        {
            l1 = r0;
            r1 = l1 + 1;
            while (r1 < s.size() && s[r1] == s[r1 - 1])
                ++r1;

            {
                int len0 = r0 - l0;
                int len1 = r1 - l1;
                ans += std::min(len0, len1);
            }
            l0 = l1;
            r0 = r1;
        }

        return ans;
    }
};
// @lc code=end
